<?php

namespace app\services\jwt;

use app\Request;

use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use think\facade\Config;

class JwtService
{
    /*
     * 获取token
     */
    public function getToken($uid,$day=30)
    {

        $key = "xuanransoftware";//自定义key值
        $payload = array(
            "iss" => '',//签发者
            "aud" => '',//接收者
            "iat" => time(),//初始时间
            "nbf" => time() + 60 * 60 * 24 * $day,//过期时间为7天
            "uid" => $uid,//前端页面传uid
        );
        $jwt = JWT::encode($payload, $key, 'HS256');
        return $jwt;
    }


    /**
     * 检查token是否过期
     * @param $token
     * @return \stdClass
     */
    public function checkToken($token)
    {
        $key = "xuanransoftware";//自定义key值
        $decoded = JWT::decode($token, new Key($key, 'HS256'));
        return $decoded;
    }

    public function getMerchantToken($id,$role_id = 0,$day=30)
    {

        $key = "xuanransoftware";//自定义key值
        $payload = array(
            "iss" => '',//签发者
            "aud" => '',//接收者
            "iat" => time(),//初始时间
            "nbf" => time() + 60 * 60 * 24 * $day,//过期时间为7天
            "staff_id" => $id,//
            'role_id'   => $role_id,

        );
        $jwt = JWT::encode($payload, $key, 'HS256');
        return $jwt;
    }
}